package com.example.black.myapplication.materia;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

import com.example.black.myapplication.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TabLayoutDemoActivity extends AppCompatActivity {

    private TabLayout tabLayout;
    private ViewPager viewPager;
    public static final String HELLO_ONE = "hello one";
    public static final String HELLO_TWO = "hello two";
    public static final String HELLO_THREE = "hello three";
    public static final String FOUR = "four";
    public static final String FIVE = "five";
    public static final String SIX = "six";
    private static List<? extends Fragment> list = new ArrayList<>(Arrays.asList(
            TabFragment.newInstance(HELLO_ONE)
            ,TabFragment.newInstance(HELLO_TWO)
            ,TabFragment.newInstance(HELLO_THREE)
            ,TabFragment.newInstance(FOUR)
            ,TabFragment.newInstance(FIVE)
            ,TabFragment.newInstance(SIX)
    ));
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_layout_demo);
        initView();
    }

    private void initView() {
        tabLayout = findViewById(R.id.tabLayout);
        tabLayout.addTab(tabLayout.newTab().setText(HELLO_ONE));
        tabLayout.addTab(tabLayout.newTab().setText(HELLO_TWO));
        tabLayout.addTab(tabLayout.newTab().setText(HELLO_THREE));
        tabLayout.addTab(tabLayout.newTab().setText(FOUR));
        tabLayout.addTab(tabLayout.newTab().setText(FIVE));
        tabLayout.addTab(tabLayout.newTab().setText(SIX));
        viewPager = findViewById(R.id.view_pager);
        viewPager.setAdapter(
                new PagerAdapter(getSupportFragmentManager())
        );
        tabLayout.setupWithViewPager(viewPager);
//        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
//        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
//            @Override
//            public void onTabSelected(TabLayout.Tab tab) {
//                viewPager.setCurrentItem(tab.getPosition());
//            }
//
//            @Override
//            public void onTabUnselected(TabLayout.Tab tab) {
//
//            }
//
//            @Override
//            public void onTabReselected(TabLayout.Tab tab) {
//
//            }
//        });

    }

    static class PagerAdapter extends FragmentStatePagerAdapter{


        public PagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return "第 "+position+" 页";
        }
    }
}
